package com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.strategy.in;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.utils.others.DateUtils;
import com.wxy.ej.flow.modules.taskflow.dto.DataDto;
import com.wxy.ej.flow.modules.taskflow.dto.NodeDto;
import com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.NodeStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class InValidateStrategy extends NodeStrategy {

    @Override
    public Result action(NodeDto node, JSONObject flowDataJson, DataDto dataDto)  throws Exception{
        log.info("InValidateStrategy: ");

        // 验证开放时间
        if (node.getDatetimeRangeValue()!=null && node.getDatetimeRangeValue().length==2) {
            log.info("datetimeRangeValue0:{}", node.getDatetimeRangeValue()[0]);
            log.info("datetimeRangeValue1:{}", node.getDatetimeRangeValue()[1]);

            Date startDate = DateTool.string2Date(node.getDatetimeRangeValue()[0], "yyyy-MM-dd HH:mm:SS");
            Date endDate = DateTool.string2Date(node.getDatetimeRangeValue()[1], "yyyy-MM-dd HH:mm:SS");
            Date now = DateTool.getCurrentDatetime("yyyy-MM-dd HH:mm:SS");

            long min0 = DateTool.between_minutes(startDate, now);
            long min1 = DateTool.between_minutes(now, endDate);

            log.info("min0:{}", min0);
            log.info("min1:{}", min1);

            if (min0 < 0) {
                return Result.builder().fault("校验开放时间：未到时间");
            }
            if (min1 < 0) {
                return Result.builder().fault("校验开放时间：已结束");
            }
        }


        return Result.builder();
    }



}
